# -*- coding: utf-8 -*-
# Time: 2022/6/5 16:56
import requests
from lxml import etree


class Spader(object):

    def __init__(self):
        self._data = {
                "日期": "",
                "地区": "",
                "项目类型": "",
                "招标采购标题":""
            }
        self.first = lambda x: x[0] if x else x

    def get_html(self, url):
        res = requests.get(url)
        if res.status_code == 200:
            return res.text
        else:
            return None

    def get_data(self, text):
        html = etree.HTML(text)
        tr_list = html.xpath('//*[@id="contentTable"]/tbody/tr')
        for tr in tr_list:
            date = self.first(tr.xpath("./td[1]/text()"))
            place = self.first(tr.xpath("./td[2]/text()"))
            content = self.first(tr.xpath("./td[3]/text()"))
            brief = self.first(tr.xpath("./td[4]/a/text()"))
            brief = str(brief).replace("\n", "").replace(" ", "")
            self._data["日期"] = date
            self._data["地区"] = place
            self._data["项目类型"] = content
            self._data["招标采购标题"] = brief
            print(self._data)
            return self._data

    def run(self):
        url = "http://zb.yfb.qianlima.com/yfbsemsite/mesinfo/zbpglist"
        text = self.get_html(url)
        if text is not None:
            self.get_data(text)

if __name__ == '__main__':
    Spader().run()





